/*
 * @Author: viivLgr 
 * @Date: 2018-03-20 17:14:39 
 * @Last Modified by: viivLgr
 * @Last Modified time: 2018-03-22 15:46:24
 */
'use strict';
require('./index.css');
var _util = require('util/util.js');
var _user = require('service/user-service.js');
var _cart = require('service/cart-service.js');
// 导航
var nav = {
    init: function(){
        this.bindEvent();
        this.loadUserInfo();
        this.loadCartCount();
        return this;
    },
    bindEvent: function(){
        // 登录
        $('.js-login').click(function(){
            console.log('login')
            _util.doLogin();
        });
        // 注册了
        $('.js-register').click(function(){
            window.location.href = './user-register.html';
        });
        // 退出点击事件
        $('.js-logout').click(function(){
            _user.logout(function(res){
                window.location.reload();
            }, function(errMsg){
                _util.errorTips(errMsg);
            });
        });
    },
    // 加载用户信息
    loadUserInfo: function(){
        _user.checkLogin(function(res){
            $('.user.not-login').hide().siblings('.user.login').show()
            .find('.username').text(res.username);
        }, function(errMsg){
            // do nothing
        });
    },
    // 加载购物车数量
    loadCartCount: function(){
        _cart.getCartCount(function(res){
            $('.nav .cart-count').text(res || 0);
        }, function(errMsg){
            $('.nav .cart-count').text(0);
        });
    }
};
module.exports = nav.init();